package com.just.recruitback.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.just.recruitback.entity.Job;
import com.just.recruitback.model.PageInfo;
import com.just.recruitback.model.Screen;
import com.just.recruitback.service.JobService;
import com.just.recruitback.service.base.ICompanyService;
import com.just.recruitback.service.base.IJobService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * @author : baibaiwucahng
 * @date : 2020/7/3  22:42
 */
@Service
public class JobServiceImpl implements JobService {

    @Resource
    private IJobService iJobService;
    @Resource
    private ICompanyService iCompanyService;

    @Override
    public Page<Job> getJobsByCompany(int companyId, PageInfo pageInfo) {
        Page<Job> page = new Page<>(pageInfo.getCount(), pageInfo.getSize());
        QueryWrapper<Job> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id", companyId);
        return iJobService.page(page, queryWrapper);
    }

    @Override
    public Job getOne(int jobId) {
        Job job = iJobService.getById(jobId);
        job.setCompany(iCompanyService.getById(job.getCompanyId()));
        return job;
    }


    @Override
    public List<Job> findJobs() {
        Random random = new Random();
        int anInt = random.nextInt(20);
        Page<Job> page = new Page<>(anInt, 5);
        iJobService.page(page);
        return page.getRecords().stream()
                .map(job -> job.setCompany(iCompanyService.getById(job.getCompanyId())))
                .collect(Collectors.toList());
    }

    @Override
    public Page<Job> screenJob(Screen screen) {
        QueryWrapper<Job> queryWrapper = new QueryWrapper<>();
        if (ObjectUtils.isNotEmpty(screen.getTitle())) {
            queryWrapper.like("title", screen.getTitle());
        }
        if (ObjectUtils.isNotEmpty(screen.getAddress())) {
            queryWrapper.like("city", screen.getAddress());
        }
        if (ObjectUtils.isNotEmpty(screen.getEmpltype())) {
            queryWrapper.eq("empltype", screen.getEmpltype());
        }
        if (ObjectUtils.isNotEmpty(screen.getWorkYear())) {
            queryWrapper.eq("work_year", screen.getWorkYear());
        }
        if (ObjectUtils.isNotEmpty(screen.getEdu())) {
            queryWrapper.eq("edu", screen.getEdu());
        }
        if (ObjectUtils.isNotEmpty(screen.getWalfare())) {
            queryWrapper.like("walfare", screen.getWalfare());
        }
        if (ObjectUtils.isNotEmpty(screen.getType())) {
            queryWrapper.like("type", screen.getPage());
        }
        Page<Job> page = new Page<>(screen.getPage().getCount(), screen.getPage().getSize());
        iJobService.page(page, queryWrapper);
        return page.setRecords(page.getRecords().stream()
                .map(job -> job.setCompany(iCompanyService.getById(job.getCompanyId())))
                .collect(Collectors.toList()));
    }
}
